package com.hongruan.dao.impl;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Repository;

import com.hongruan.dao.BlobDao;

@Repository
public class BlobDaoImpl implements BlobDao {

	private Connection oracleCon = null;

	public BlobDaoImpl() {
	}

	public Connection getConnection() {
		oracleCon = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			oracleCon = DriverManager.getConnection(
					"jdbc:oracle:thin:@93.40.16.48:1521:taxarch", "tax_arch",
					"passok");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return oracleCon;
	}

	public void closeConnection(Connection conn) {
		try {
			if (conn != null) {
				if (!conn.isClosed()) {
					conn.close();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public byte[] getImage(String colname, String nsrxx,int width, int height) {
		this.getConnection();
		BufferedInputStream ins;// 取得BLOB的IO流
		byte[] bt = null;
		Blob bo = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select "
				+ colname
				+ " from arch_jbxx jbxx where jbxx.daxh = ? or jbxx.nsrsbh = ? or jbxx.nsrmc = ? ";
		try {
			ps = oracleCon.prepareStatement(sql);
			ps.setString(1, nsrxx);
			ps.setString(2, nsrxx);
			ps.setString(3, nsrxx);
			rs = ps.executeQuery();
			if (rs.next()) {
				bo = rs.getBlob(colname);
				if (bo != null) {
					try {
						ins = new BufferedInputStream(bo.getBinaryStream());
						int bufferSize = (int) bo.length();// 取得BLOB的长度
						bt = new byte[bufferSize];
						try {
							ins.read(bt, 0, bufferSize);
							ins.close();
							bt = this.ChangeImgSize(bt, width, height);
						} catch (IOException e) {
							e.printStackTrace();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				oracleCon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return bt;
	}

	private byte[] ChangeImgSize(byte[] data, int nw, int nh) {
		byte[] newdata = null;
		try {
			BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));
			int w = bis.getWidth();
			int h = bis.getHeight();
			double sx = (double) nw / w;
			double sy = (double) nh / h;
			AffineTransform transform = new AffineTransform();
			transform.setToScale(sx, sy);
			AffineTransformOp ato = new AffineTransformOp(transform, null);
			BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
			ato.filter(bis, bid);
			// 转换成byte字节
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(bid, "jpeg", baos);
			newdata = baos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return newdata;
	}

}
